<template>
  <div class="app-container">
    <el-button
      size="small"
      type="cyan"
      icon="el-icon-edit"
      @click="handleAdd"
      v-hasPermi="['issueyear:setyear:add']"
    >添加</el-button>
    <el-row style="margin-top: 20px" :gutter="20">
      <!--开放的年度数据-->
      <el-col :span="24" :xs="24">
        <el-table v-loading="loading" :data="yearJsonList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="开放的年度编号" align="center" prop="yearId" />
          <el-table-column label="开放的年度时间段" align="center" prop="duringYear" :show-overflow-tooltip="true" />
          <el-table-column
            label="操作"
            align="center"
            width="180"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope">
              <el-button
                size="small"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['issueyear:setyear:remove']"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label-width="240" prop="duringYear">
              <el-tag>提示: 年度之间连续，没有开放贷款的年度  格式: 如2020-2021、2021-2022</el-tag>
              <el-input style="width: 417px; margin-top: 10px" v-model="form.duringYear" placeholder="请输入开放的年度时间段" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
  import { listYearJsonData, getYearJson, addYearJson, updateYearJson, delYearJson } from "@/api/yearJson/yearJson";
  import "@riophae/vue-treeselect/dist/vue-treeselect.css";

  export default {
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 总条数
        total: 0,
        // 开放的年度表格数据
        yearJsonList: null,
        // 弹出层标题
        title: "",
        // 部门树选项
        // 是否显示弹出层
        open: false,
        // 表单参数
        form: {},
        // 开放的年度导入参数
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10
        },
        // 表单校验
        rules: {

        }
      };
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询开放的年度列表 */
      getList() {
        this.loading = true;
        listYearJsonData(this.queryParams).then(res => {
          console.log(res.rows)
            this.yearJsonList = res.rows
            this.total = res.total
            this.loading = false
          }
        );
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          yearId: undefined,
          duringYear: undefined,
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.page = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.yearId);
        this.single = selection.length != 1;
        this.multiple = !selection.length;
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true
        this.title = "添加开放的年度";
      },
      /** 提交按钮 */
      submitForm: function() {
        this.$refs["form"].validate(valid => {
          if (valid) {
              let arr = []
              listYearJsonData().then(res => {
                for (let i in res.rows) {
                  arr.push(res.rows[i].duringYear)
                }
                console.log(arr)
                if (arr.includes(this.form.duringYear)) {
                  this.msgInfo('该年度已经存在!')
                  return
                } else {
                  console.log(this.form)
                  addYearJson(this.form).then(response => {
                    if (response.code === 200) {
                      this.msgSuccess("新增成功");
                      this.open = false;
                      this.getList();
                    }
                  });
                }
              })
           }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const yearIds = row.yearId || this.ids;
        this.$confirm('是否确认删除开放的年度编号为"' + yearIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delYearJson(yearIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
      },
    }
  };
</script>
